/*----------------------------------------
	1.1.4) 用户管理
	----------------------------------------*/
	var Admin;
	if (!Admin) Admin = {};
	Admin.um = {};
	
	/**
	 * main gui of user manage
	 */
	Admin.um.main = function()
	{
		Admin.um.list("", "", "", "", "");
	}
	
	/**
	 * list users
	 * @param key_words
	 * @param category
	 * @param offset
	 */
	Admin.um.list = function(key_words, user_category, function_FLAG, confirm_FLAG, offset)
	{
		$(".content_block").spin();
		$.ajax({
			url: um_path + "user_list",
			data: {key_words: key_words, user_category: user_category, function_FLAG: function_FLAG, confirm_FLAG: confirm_FLAG, offset: offset},
			success: function(html) {
				$(".content_block").spin(false);
				$(".content_block").html(html);
				var user_num = $("#user_num").val();
				var per_page = parseInt($("#per_page").val());
				if (user_num > per_page) {
					function pageSelectCallback(page_index, jq){
						var offset = page_index * per_page;
						Admin.um.offset = offset;
						Admin.um.list(key_words, user_category, function_FLAG, confirm_FLAG, offset);
					}
					
					$("#Pagination").css({ "display": "inline"});
					
					$(function() {
						var count = user_num;
						$("#Pagination").pagination(count, {
							current_page: (offset / per_page),
							num_edge_entries: 2,
							num_display_entries: 8,
							callback: pageSelectCallback,
							items_per_page: per_page
						});
					});
					
					$("#Pagination a").removeAttr("href");
				}
			}
		});
	}
	
	/**
	 * delete user
	 * @param id
	 */
	Admin.um.delete_by_id = function(id)
	{
		$(".content_block").spin();
		$.ajax({
			url: um_path + "user_delete",
			dataType: 'json',
			data: {id: id},
			success: function(data) {
				$(".content_block").spin(false);
			}
		});
	}
	
	/**
	 * modify user info
	 * @param id
	 */
	Admin.um.to_modify = function(id)
	{
		$(".content_block").spin();
		$.ajax({
			url: um_path + "user_modify",
			data: {id: id},
			success: function(html) {
				$(".content_block").spin(false);
				$(".content_block").html(html);
			}
		});
	}
	
	/**
	 * submit modify user info
	 * @param id
	 * @param password
	 * @param email
	 */
	Admin.um.submit_modify = function(id, password, email)
	{
		$(".content_block").spin();
		$.ajax({
			url: um_path + "user_modify_submit",
			dataType: 'json',
			data: {id: id, password: password, email: email},
			success: function(data) {
				$(".content_block").spin(false);
			}
		});
	}
	
	/**
	 * submit verify user info
	 * @param id
	 * @param confirm_FLAG
	 */
	Admin.um.submit_verify = function(id, confirm_FLAG)
	{
		$(".content_block").spin();
		$.ajax({
			url: um_path + "user_verify_submit",
			dataType: 'json',
			data: {id: id, confirm_FLAG: confirm_FLAG},
			success: function(data) {
				$(".content_block").spin(false);
				Admin.um.to_modify(id);
			}
		});
	}
	
	Admin.um.create = function(user_name, password, category_id)
	{
		$("#um_create_newer_dialog").spin();
		$.ajax({
			url: um_path + "user_create",
			dataType: 'json',
			data: {user_name: user_name, password: password, category_id: category_id},
			success: function(data) {
				Admin.um.create_is_succeed = false;
				$("#um_create_newer_dialog").spin(false);
				switch (data)
				{
				case 'null_username_error':
					$('#create_msg').html("<strong style='background: url(/images/icon/attention.png) no-repeat; color: #f60;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入用户名</strong>");
					break;
				case 'null_password_error':
					$('#create_msg').html("<strong style='background: url(/images/icon/attention.png) no-repeat; color: #f60;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入密码</strong>");
					break;
				case 'null_admin_category_error':
					$('#create_msg').html("<strong style='background: url(/images/icon/attention.png) no-repeat; color: #f60;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择管理员类型</strong>");
					break;
				case 'name_length_error':
					$('#create_msg').html("<strong style='background: url(/images/icon/attention.png) no-repeat; color: #f60;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名长度应为5~16字节</strong>");
					break;
				case 'name_pattern_error':
					$('#create_msg').html("<strong style='background: url(/images/icon/attention.png) no-repeat; color: #f60;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名格式不对<br />（字母开头，允许字母数字下划线）</strong>");
					break;
				case 'password_length_error':
					$('#create_msg').html("<strong style='background: url(/images/icon/attention.png) no-repeat; color: #f60;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录密码长度应为4~10字节</strong>");
					break;
				case 'username_duplicate_error':
					$('#create_msg').html("<strong style='background: url(/images/icon/error.png) no-repeat;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;该用户已存在</strong>");
					break;
				case 'succeed':
					Admin.um.list(Admin.um.keywords, Admin.um.user_category, Admin.um.function_FLAG, Admin.um.confirm_FLAG, "");
					Admin.um.create_is_succeed = true;
					break;
				}
			}
		});
	}
	
	Admin.um.keywords;
	Admin.um.user_category;
	Admin.um.function_FLAG;
	Admin.um.confirm_FLAG;
	Admin.um.offset;
	Admin.um.create_is_succeed;
	
	// listen to 'search' button
	$("#search_submit").live('click', function() {
		Admin.um.keywords = $('#keywords').val();
		Admin.um.user_category = $('#user_category').val();
		Admin.um.function_FLAG = $('#function_FLAG').val();
		Admin.um.confirm_FLAG = $('#confirm_FLAG:visible').val();
		Admin.um.list(Admin.um.keywords, Admin.um.user_category, Admin.um.function_FLAG, Admin.um.confirm_FLAG, "");
	});
	
	// listen to 'create' button
	$("#create_new").live('click', function() {
		$("#create_msg").empty();
		$("#um_creat_newer_dialog :text").val("");
		$("#um_creat_newer_dialog select").val("");
		$("#um_creat_newer_dialog").dialog("open");
	});
	
	// listen to 'verify' button
	$("#submit_verify").live('click', function() {
		$("#um_submit_verify_dialog").dialog("open");
	});
	
	// listen to 'modify' button
	$(".um.modify").live('click', function() {
		Admin.um.to_modify($(this).attr("id"));
	});
	
	// listen to 'delete' button
	$(".um.delete").live('click', function() {
		if (confirm('确认删除该帐号？')) {
			Admin.um.delete_by_id($(this).attr("id"));
			Admin.um.list(Admin.um.keywords, Admin.um.user_category, Admin.um.function_FLAG, Admin.um.confirm_FLAG, Admin.um.offset);
		} else {
			return false;
		}
	});
	
	// listen to 'submit modify' button
	$(".um#submit_modify").live('click', function() {
		Admin.um.submit_modify($("#user_id").val(), $("#password").val(), $("#email").val());
		Admin.um.list(Admin.um.keywords, Admin.um.user_category, Admin.um.function_FLAG, Admin.um.confirm_FLAG, Admin.um.offset);
	});
	
	// listen to 'back' button
	$(".um#back").live('click', function() {
		Admin.um.list(Admin.um.keywords, Admin.um.user_category, Admin.um.function_FLAG, Admin.um.confirm_FLAG, Admin.um.offset);
	});
	
	